# 3.实现一个 atoi 函数，使其能将字符串转换成整数
# def my_atoi(s: str) -> int:
#     # 实现代码区域
# print(my_atoi("42"))  # 输出: 42
# print(my_atoi("   -42"))  # 输出: -42
# print(my_atoi("4193 abcd case"))  # 输出: 4193  此题不允许使用int()


#eval() 可以将字符串里的进行运算，还可以转换成int类型
# a=eval("-1*2")
# print(type(a))
# print(a)

# a="4193 abcd case"
# s=a.split(" ")
# s_1=s[0]
# s_2=eval(s_1)
# print(s)
# print(s_1)
# print(type(s_1))
# print(type(s_2))

def my_atoi(s: str) -> int:
    # 实现代码区域
    s = s.strip()
    if not s:
        return 0
    sign = 1
    if s[0] == '-':
        sign = -1
        s = s[1:]       #从第二个数开始
    elif s[0] == '+':
        s = s[1:]
    res = 0
    for c in s:
        if not c.isdigit():
            break
        res = res * 10+ eval(c)
    res =res*sign
    return res

print(my_atoi("4193 abcd case"))
print(my_atoi("    -42"))
print(my_atoi("42"))